Refresh.Web.DefaultToolbarSettings = {
	theme: Refresh.defaultTheme,
	cssPrefix: '',
	title: '',
	language: 'en-US',
	useTable: false,
	target: null,
	config: null
};

Refresh.Web.Toolbar = Class.create();
Refresh.Web.Toolbar.prototype = {

	visibleElement: null,
	
	_items: null,
	
	initialize: function(id, settings) {
		this.id = id;
		this.settings = Object.extend(Refresh.Web.DefaultToolbarSettings, settings);
		this._items = new Array();
		
		this.settings.useTable = (Refresh.Web.BrowserInfo.isOpera && this.settings.theme == 'office12') ? true :false;
		
				
		// create HTML		
		this.node = null;
		this._currentRow = null;
		this._currentBody = null;
				
		// DRAW container
		if (this.node == null) {
			
			// container
			if (this.settings.target) {
				
				this.node = Refresh.Web.HTML.append(this.settings.target, 'div', {id:this.id, className: this.settings.cssPrefix + 'toolbar'});
			} else {
				this.node = Refresh.Web.HTML.create('div', {id:this.id, className: this.settings.cssPrefix + 'toolbar'});
			}
			
			if (this.settings.useTable) {
				this._table = Refresh.Web.HTML.append(this.node, 'table', {cellPadding: 0, cellSpacing: 0, border: 0});
				this._tbody = Refresh.Web.HTML.append(this._table, 'tbody');
				this._row = Refresh.Web.HTML.append(this._tbody, 'tr');			

			}
		}
		
		for (var groupIndex=0; groupIndex < this.settings.groups.length; groupIndex++) {
			// create group
			var group = this.settings.groups[groupIndex];
			
			this._drawGroup(group.title);
			
			for (var itemIndex=0; itemIndex < group.items.length; itemIndex++) {
				
				var item = group.items[itemIndex];
				if (item == null) {
					continue;
				}
				
				var command = Refresh.Web.EditorCommands.getCommand(item.name);
					
				// check for special types (break, separator)
				if (item.name == 'break') {					
					this._closeRow();
					this._createBreak();
					continue;
				} else if (item.name == 'separator') {					
					this._closeRow();
					continue;
				}
				
				if (!command) { 
					
					continue; 
				}
				
				// make sure the right row is open
				if (command.type.toLowerCase().indexOf('button') > -1) {
					if (this._currentRow == null || this._currentRow.className.indexOf('ButtonRow') == -1) {
						this._openRow('ButtonRow');
					}
				} else if (command.type.toLowerCase() == 'combobox') {
					if (this._currentRow == null || this._currentRow.className.indexOf('ListRow') == -1) {
						this._openRow('ListRow');				
					}
				}				
				
				var itemID = this.id + '_g' + groupIndex + '_i' + itemIndex;				
				
				// create item from name and its subitems (for ComboBox)
				var toolbarItem = Refresh.Web.EditorCommands.createItem(
					itemID, 
					item.name, 
					(item.settings) ? item.settings : null,
					item.items,
					this);			
				
				//
				this._items.push( toolbarItem );
			
				this._currentRow.appendChild(toolbarItem.node);
			}
			
			this._closeRow();
			this._closeGroup();
		}

	},

	
	// DRAWING
	_drawGroup: function(title) {
			var groupDiv = null;
			if (this.settings.useTable) {
				var cell = Refresh.Web.HTML.append(this._row, 'td');	
				groupDiv = Refresh.Web.HTML.append(cell, 'div', {className: this.settings.cssPrefix + 'toolbar_Group'});
				var brk = Refresh.Web.HTML.append(cell, 'div', {});	
				brk.style.clear = 'both';				
			} else {
				groupDiv = Refresh.Web.HTML.append(this.node, 'div', {className: this.settings.cssPrefix + 'toolbar_Group'});	
			}

	
			// create header
			var header = Refresh.Web.HTML.append(groupDiv, 'div', {className: this.settings.cssPrefix + 'toolbar_Header'});		
			//header.innerHTML = '<span>' + title + '</span>';
			Refresh.Web.HTML.append(header, 'span', {}, title);	
			
			// body
			this._currentBody = Refresh.Web.HTML.append(groupDiv, 'div', {className: this.settings.cssPrefix + 'toolbar_Body'});
			
			// create header
			var footer = Refresh.Web.HTML.append(groupDiv, 'div', {className: this.settings.cssPrefix + 'toolbar_Footer'});		
			//footer.innerHTML = '<span>' + title + '</span>';	
			Refresh.Web.HTML.append(footer, 'span', {}, title);			
	},
	
	_closeGroup: function() {
		var breakDiv = Refresh.Web.HTML.append(this._currentBody, 'div' );		
		breakDiv.className = 'toolbar_Break';
	},
	
	_openRow: function(classname) {
		if (this._currentRow != null) {
			this._closeRow();
		}

		var newRow = document.createElement('div');		
		this._currentBody.appendChild(newRow);
		newRow.className = this.settings.cssPrefix + 'toolbar_' + classname;
		
		this._currentRow = newRow;
	},
	
	_closeRow: function() {
		this._currentRow = null;
	},
	
	_createBreak: function() {
		if (this._currentRow != null) {
			this.closeRow();
		}	
		var brk = document.createElement('div');
		this._currentBody.appendChild(brk);
		brk.className = this.settings.cssPrefix + 'toolbar_Break';
	},	
	
	
	// EDITORS
	_editor: null,
	
	associateEditor : function(target) {
		this._editor = target;
		if (target.associateToolbar) {
			target.associateToolbar(this);
		}
	},	
	
	getActiveEditor: function() {
		return this._editor;
	},
	
	update: function(src) {
		
		
		var ftb = this.getActiveEditor();
		
		
		
		if (ftb==null) {
			return;
			
		}
		
		for (var i=0; i<this._items.length; i++) {		
			this._items[i].setState(ftb);
		}
	
	}	
};